<?php
declare (strict_types=1);

use app\common\model\User;
use think\facade\Db;
use think\helper\Str;

/**
 * 整理菜单树方法
 */
function prepareMenu($param, $pid = 1)
{
    $parent = []; //父类
    $child = [];  //子类
    foreach ($param as $key => $vo) {

        if ($vo['pid'] == $pid) {
            $vo['href'] = '#';
            $parent[] = $vo;
        } else {
            $vo['href'] = strstr($vo['name'], '/') ? url($vo['name'])->build() : url($vo['name'] . '/index')->build();
            $child[] = $vo;
        }
    }
    foreach ($parent as $key => $vo) {
        foreach ($child as $k => $v) {

            if ($v['pid'] == $vo['id']) {

                $parent[$key]['child'][] = $v;
            }

        }
    }
    unset($child);

    return $parent;
}

function getCount($fetchSql)
{
    $fetchSql = "SELECT count(*) as tp_count " . strstr($fetchSql, "FROM");
    return Db::query($fetchSql)[0]['tp_count'];
}

function queryIp($ip)
{
    $url = "http://whois.pconline.com.cn/jsFunction.jsp?ip=" . $ip;
    $ch = curl_init();
    //设置选项，包括URL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //执行并获取HTML文档内容
    $output = curl_exec($ch);
    //释放curl句柄
    curl_close($ch);
    $info = iconv('GB2312', 'UTF-8', $output); //因为是js调用 所以接收到的信息为字符串，注意编码格式

    return get_between($info, "('", "',");
}

function get_between($input, $start, $end)
{
    return substr($input, strlen($start) + strpos($input, $start), (strlen($input) - strpos($input, $end)) * (-1));
}

function getWeekTwo($value)
{
    return match (intval($value)) {
        1 => '一',
        2 => '二',
        3 => '三',
        4 => '四',
        5 => '五',
        6 => '六',
        default => '日'
    };
}

function handleImg($src)
{
    if (empty($src)) {
        return '';
    }
    if (is_array($src)) {
        foreach ($src as &$v) {
            $v = request()->scheme() . '://' . $_SERVER['SERVER_NAME'] . $v;
        }
        return $src;
    } else {
        return request()->scheme() . '://' . $_SERVER['SERVER_NAME'] . $src;
    }
}

/**
 * 格式化富文本内容
 */
function handleRichText($content)
{
    $url = request()->scheme() . '://' . $_SERVER['SERVER_NAME'] . '/uploads/';
    $content = htmlspecialchars_decode(str_replace('/uploads/', $url, $content));
    return str_replace('\\', '', $content);
}

/**
 * 根据一定数量元素数组随机组合
 */
function randomArray($arr, $length)
{
    shuffle($arr);
    return array_chunk($arr, $length);
}

/**
 * 随机获取数组中一定数量的数据
 */
function getRandomArrayElements($array, $count)
{
    if ($count <= 0) return [];
    shuffle($array);
    $keys = array_rand($array, $count);
    $results = [];
    if ($count > 1) {
        foreach ($keys as $key) {
            $results[] = $array[$key];
        }
    } else {
        $results[] = $array[$keys];
    }

    return $results;
}

/**
 * 截取字符串
 */
function cut_str($str, $len)
{
    if (strlen($str) > $len) {
        $str = mb_substr($str, 0, $len) . '...';
    }

    return $str;
}

/**
 * 隐藏手机号
 */
function hideMobile($mobile)
{
    return preg_replace('/^(\d{3})(\d{4})(\d{4})$/', '\1****\3', $mobile);
}

/*
 * 隐藏姓名
 */
function hideName($name)
{
    $doubleSurname = [
        '欧阳', '太史', '端木', '上官', '司马', '东方', '独孤', '南宫',
        '万俟', '闻人', '夏侯', '诸葛', '尉迟', '公羊', '赫连', '澹台', '皇甫', '宗政', '濮阳',
        '公冶', '太叔', '申屠', '公孙', '慕容', '仲孙', '钟离', '长孙', '宇文', '司徒', '鲜于',
        '司空', '闾丘', '子车', '亓官', '司寇', '巫马', '公西', '颛孙', '壤驷', '公良', '漆雕', '乐正',
        '宰父', '谷梁', '拓跋', '夹谷', '轩辕', '令狐', '段干', '百里', '呼延', '东郭', '南门', '羊舌',
        '微生', '公户', '公玉', '公仪', '梁丘', '公仲', '公上', '公门', '公山', '公坚', '左丘', '公伯',
        '西门', '公祖', '第五', '公乘', '贯丘', '公皙', '南荣', '东里', '东宫', '仲长', '子书', '子桑',
        '即墨', '达奚', '褚师', '吴铭'
    ];
    $surname = mb_substr($name, 0, 2);
    if (in_array($surname, $doubleSurname)) {
        $name = mb_substr($name, 0, 2) . str_repeat('*', (mb_strlen($name, 'UTF-8') - 2));
    } else {
        $name = mb_substr($name, 0, 1) . str_repeat('*', (mb_strlen($name, 'UTF-8') - 1));
    }

    return $name;
}

/**
 * 生成随机会员编号
 */
function generate($length = 6, $prefix = '', $type = 1)
{
    $code = Str::random($length, $type, $prefix);
    $is = User::where(['code' => $code])->count();

    return $is ? generate(6, $prefix, $type) : $code;
}

/**
 * 生成随机数
 */
function generateRandom($length)
{
    return Str::random($length);
}

/**
 * 审核状态
 */
function getExamineStatus($key = '')
{
    $arr = [
        1 => '待审核',
        2 => '已通过',
        3 => '已驳回'
    ];
    if ($key) {
        return $arr[$key];
    }

    return $arr;
}

/**
 * 位置
 */
function getPosition($key = '')
{
    $arr = [
        1 => '首页'
    ];
    if ($key) {
        return $arr[$key];
    }

    return $arr;
}

/**
 * 奖金类型
 */
function getBonusTitle($type)
{
    if ($type == 1) {
        $arr = [
            1 => '推荐收益',
            2 => '团队收益',
            3 => '平级收益'
        ];
    } else {
        $arr = [
            'sum_1' => '推荐收益',
            'sum_2' => '团队收益',
            'sum_3' => '平级收益'
        ];
    }

    return $arr;
}

/**
 * 支付方式
 */
function get_pay_way($value)
{
    return empty($value) ? '-' : match ($value) {
        'bank' => '银行卡',
        'wechat' => '微信',
        'alipay' => '支付宝',
        default => \app\common\model\Assets::getFields(3)[$value]
    };
}
